using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
//卡牌的状态，正面、背面
public enum CardState {
    Front,
    Back
}
 
public class EventCard : MonoBehaviour {
 
    public GameObject mFront;       //卡牌正面
    public GameObject mBack;        //卡牌的背面
    public CardState mCardState = CardState.Front;  //卡牌当前的状态，是正面还是背面
    public float mTime = 0.3f;
    public bool isActive = false;       //true代表正在执行翻转，不许被打断
 
    /// <summary>
    /// 初始化卡牌角度，根据mCardState
    /// </summary>
    public void Init() {
        if (mCardState == CardState.Front)
        {
            mFront.transform.eulerAngles = Vector3.zero;
            mBack.transform.eulerAngles = new Vector3(0, 90, 0);
        }
        else
        {
            mBack.transform.eulerAngles = Vector3.zero;
            mFront.transform.eulerAngles = new Vector3(0, 90, 0);
        }
    }
 
	void Start () {
        Init();
        StartBack();


    }
 
    //开始向后转
    public void StartBack()
    {
        if (isActive)
        {
            return;
        }
        StartCoroutine(ToBack());
    }
 
    //开始前转
    public void StartFront()
    {
        if (isActive)
        {
            return;
        }
        StartCoroutine(ToFront());
    }
 
    /// <summary>
    /// 翻转到背面
    /// </summary>
    /// <returns></returns>
    IEnumerator ToBack()
    {
        isActive = true;
        mFront.transform.DORotate(new Vector3(0, 90, 0), mTime);
        for (float i = mTime; i >= 0; i-=Time.deltaTime)
        {
            yield return 0;
        }
        mBack.transform.DORotate(new Vector3(0, 0, 0), mTime);
        isActive = false;
    }
 
    /// <summary>
    /// 翻转到正面
    /// </summary>
    /// <returns></returns>
    IEnumerator ToFront()
    {
        isActive = true;
        mBack.transform.DORotate(new Vector3(0, 90, 0), mTime);
        for (float i = mTime ; i >= 0; i-= Time.deltaTime)
        {
            yield return 0;
        }
        mFront.transform.DORotate(new Vector3(0, 0, 0), mTime);
        isActive = false;
    }
 
}

